package cn.patpang.core.tool;

import org.apache.commons.beanutils.BeanUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author AngPPX Date 2017/5/23 11:11 Email angppx@qq.com Description
 */
public class CollectionTool {

	/**
	 * 快速创建map
	 *
	 * @param key
	 * @return
	 */
	public static Map<String, Object> simpleMap(String key, Object value) {
		return Collections.singletonMap(key, value);
	}

	/**
	 * list 指定属性转map
	 *
	 * @param list
	 * @param key
	 * @return
	 */
	public static Map<String, ? extends Object> list2Map(List<? extends Object> list, Class clazz, String key) {
		Map<String, Object> reMap;

		if (null != list && list.size() > 0 && BeanTool.hasField(clazz, key)) {
			reMap = new HashMap<String, Object>();
			for (Object obj : list) {
				try {
					reMap.put(BeanUtils.getSimpleProperty(obj, key), obj);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return reMap.size() > 0 ? reMap : null;
		}

		return null;
	}

}
